package cn.shengchao.examstar.knowledge.infrastructure.repository.impl;

import cn.shengchao.examstar.knowledge.domain.entity.KnowledgePointMastery;
import cn.shengchao.examstar.knowledge.domain.repository.KnowledgePointMasteryRepository;
import cn.shengchao.examstar.knowledge.infrastructure.converter.KnowledgePointMasteryConverter;
import cn.shengchao.examstar.knowledge.infrastructure.dao.TKnowledgePointMasteryMapper;
import cn.shengchao.examstar.knowledge.infrastructure.dao.po.TKnowledgePointMastery;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 知识点掌握度仓储实现类
 */
@Repository
@RequiredArgsConstructor
public class KnowledgePointMasteryRepositoryImpl implements KnowledgePointMasteryRepository {

    private final TKnowledgePointMasteryMapper masteryMapper;
    private final KnowledgePointMasteryConverter masteryConverter;

    @Override
    public KnowledgePointMastery save(KnowledgePointMastery knowledgePointMastery) {
        TKnowledgePointMastery po = masteryConverter.toPO(knowledgePointMastery);
        if (po.getId() == null) {
            masteryMapper.insert(po);
        } else {
            masteryMapper.updateById(po);
        }
        return masteryConverter.toEntity(po);
    }

    @Override
    public Optional<KnowledgePointMastery> findByUserIdAndKnowledgePointId(Long userId, Long knowledgePointId) {
        LambdaQueryWrapper<TKnowledgePointMastery> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TKnowledgePointMastery::getUserId, userId)
                .eq(TKnowledgePointMastery::getKnowledgePointId, knowledgePointId);
        TKnowledgePointMastery po = masteryMapper.selectOne(wrapper);
        return Optional.ofNullable(po).map(masteryConverter::toEntity);
    }

    @Override
    public List<KnowledgePointMastery> findByUserId(Long userId) {
        LambdaQueryWrapper<TKnowledgePointMastery> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TKnowledgePointMastery::getUserId, userId);
        List<TKnowledgePointMastery> poList = masteryMapper.selectList(wrapper);
        return poList.stream().map(masteryConverter::toEntity).collect(Collectors.toList());
    }

    @Override
    public List<KnowledgePointMastery> findByStudyPlanId(Long studyPlanId) {
        LambdaQueryWrapper<TKnowledgePointMastery> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TKnowledgePointMastery::getStudyPlanId, studyPlanId);
        List<TKnowledgePointMastery> poList = masteryMapper.selectList(wrapper);
        return poList.stream().map(masteryConverter::toEntity).collect(Collectors.toList());
    }

    @Override
    public void batchUpdateMastery(List<KnowledgePointMastery> masteryList) {
        for (KnowledgePointMastery mastery : masteryList) {
            save(mastery);
        }
    }

    @Override
    public void delete(Long id) {
        TKnowledgePointMastery po = new TKnowledgePointMastery();
        po.setId(id);
        po.setStatus(0);  // 标记删除
        masteryMapper.updateById(po);
    }
} 